﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Tax_NewSubClass : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strSQL = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _BindTaxType();
        }
    }
    private void _BindTaxType()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TAXCATEGORY","1")
                                };
        DataTable dttaxtype = DU.getdatausingprocedure("USP_ACC_GetTaxTypeBasedOnTaxCategory", sparam);
        ddlTaxType.DataSource = dttaxtype;
        ddlTaxType.DataTextField = "TAXTYPE";
        ddlTaxType.DataValueField = "TAXTYPEID";
        ddlTaxType.DataBind();
        ddlTaxType.Items.Insert(0, new ListItem("-Select-", "0"));
    }
    private void _BindMainClass(string strTaxTypeId)
    {
        ddlMainClass.Items.Clear();       
        if (strTaxTypeId != string.Empty && strTaxTypeId != "0")
        {
            strSQL = "SELECT MAINCLASSID,MAINCLASSNAME FROM ACC_TAXMAINCLASS WHERE TAXTYPEID='" + strTaxTypeId + "' ";
            DataTable DTMainClass = DU.getdata(strSQL);
            if (DTMainClass.Rows.Count > 0)
            {
                if (DTMainClass.Rows.Count == 1)
                {
                    ddlMainClass.Items.Insert(0, new ListItem("", DTMainClass.Rows[0]["MAINCLASSID"].ToString()));
                    ddlMainClass.Enabled = false;                    
                }
                else if (DTMainClass.Rows.Count > 1)
                {
                    ddlMainClass.Enabled = true;
                    ddlMainClass.DataSource = DTMainClass;
                    ddlMainClass.DataTextField = "MAINCLASSNAME";
                    ddlMainClass.DataValueField = "MAINCLASSID";
                    ddlMainClass.DataBind();
                    ddlMainClass.Items.Insert(0, new ListItem("-Select-", "0"));
                }


            }
            else
            {
                ddlMainClass.Items.Insert(0, new ListItem("", "0"));
                ddlMainClass.Enabled = false;
            }
        }
        else
        {
            ddlMainClass.Items.Insert(0, new ListItem("", "0"));
            ddlMainClass.Enabled = false;

        }
    }
    
    protected void ddlTaxType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlShortName.Items.Clear();
        _BindMainClass(ddlTaxType.SelectedValue);    
        if (ddlMainClass.Items.Count == 1)
        {
            ddlMainClass.Enabled = false;           
            _BindShortName(ddlTaxType.SelectedValue);
        }       
    }
    protected void ddlMainClass_SelectedIndexChanged(object sender, EventArgs e)
    { 
        _BindShortName(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue);        
       
    }
    
    private void _BindShortName(string strTaxTypeId, string strMainClassId)
    {
        ddlShortName.Items.Clear();
        if (strTaxTypeId != string.Empty)
        {
            DataTable dtsubclass = DU.getdata("SELECT TAXMASTERID,SHORTNAME FROM UVW_ACC_TaxMaster WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='" + strMainClassId + "'");
            ddlShortName.DataSource = dtsubclass;
            ddlShortName.DataTextField = "SHORTNAME";
            ddlShortName.DataValueField = "TAXMASTERID";
            ddlShortName.DataBind();
        }

    }
    private void _BindShortName(string strTaxTypeId)
    {
        ddlShortName.Items.Clear();
        if (strTaxTypeId != string.Empty)
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TAXTYPEID",strTaxTypeId)
                                    };
            DataTable DTShortName = DU.getdatausingprocedure("USP_ACC_GetTaxShortName", sparam);
            ddlShortName.DataSource = DTShortName;
            ddlShortName.DataTextField = "SHORTNAME";
            ddlShortName.DataValueField = "TAXMASTERID";
            ddlShortName.DataBind();
        }
    }
}
